package com.joker.orange.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.joker.common.core.page.TableDataInfo;
import com.joker.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.joker.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.joker.orange.domain.bo.RedemptionCodeBo;
import com.joker.orange.domain.vo.RedemptionCodeVo;
import com.joker.orange.domain.RedemptionCode;
import com.joker.orange.mapper.RedemptionCodeMapper;
import com.joker.orange.service.IRedemptionCodeService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 兑换码Service业务层处理
 *
 * @author Joker
 * @date 2024-07-27
 */
@RequiredArgsConstructor
@Service
public class RedemptionCodeServiceImpl implements IRedemptionCodeService {

    private final RedemptionCodeMapper baseMapper;

    /**
     * 查询兑换码
     */
    @Override
    public RedemptionCodeVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询兑换码列表
     */
    @Override
    public TableDataInfo<RedemptionCodeVo> queryPageList(RedemptionCodeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<RedemptionCode> lqw = buildQueryWrapper(bo);
        Page<RedemptionCodeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询兑换码列表
     */
    @Override
    public List<RedemptionCodeVo> queryList(RedemptionCodeBo bo) {
        LambdaQueryWrapper<RedemptionCode> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<RedemptionCode> buildQueryWrapper(RedemptionCodeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<RedemptionCode> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, RedemptionCode::getUserId, bo.getUserId());
        lqw.like(StringUtils.isNotBlank(bo.getCodeName()), RedemptionCode::getCodeName, bo.getCodeName());
        lqw.eq(StringUtils.isNotBlank(bo.getGiveTime()), RedemptionCode::getGiveTime, bo.getGiveTime());
        lqw.eq(StringUtils.isNotBlank(bo.getMemberType()), RedemptionCode::getMemberType, bo.getMemberType());
        lqw.eq(StringUtils.isNotBlank(bo.getUseStatus()), RedemptionCode::getUseStatus, bo.getUseStatus());
        lqw.eq(bo.getRedemptionTime() != null, RedemptionCode::getRedemptionTime, bo.getRedemptionTime());
        return lqw;
    }

    /**
     * 新增兑换码
     */
    @Override
    public Boolean insertByBo(RedemptionCodeBo bo) {
        RedemptionCode add = BeanUtil.toBean(bo, RedemptionCode.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改兑换码
     */
    @Override
    public Boolean updateByBo(RedemptionCodeBo bo) {
        RedemptionCode update = BeanUtil.toBean(bo, RedemptionCode.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(RedemptionCode entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除兑换码
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
